import 'package:dk_flutter_base/dk_ui.dart';
import 'package:dk_flutter_base/dk_core.dart';
import 'package:dk_flutter_module_sale/common/config/page_base_common.dart';
import 'package:dk_flutter_module_sale/common/user_track/user_track.dart';
import 'package:dk_flutter_module_sale/phoenix/api/revision_main_api.dart';
import 'package:dk_flutter_module_sale/phoenix/tools/bean/bean_tools_category.dart';
import 'package:dk_flutter_module_sale/phoenix/tools/bean/bean_tools_item.dart';
import 'package:dk_flutter_module_sale/phoenix/tools/bean/event_bus_select_tool_category.dart';
import 'package:dk_flutter_module_sale/phoenix/tools/widget/widget_tools_category_button.dart';
import 'package:dk_flutter_module_sale/phoenix/tools/widget/widget_tools_item.dart';

// @ARoute(url: CommonWidgetRoute.COMMON_TOOLS)
class HomeAllToolsPage extends CommonPage {
  // final WidgetOption option;
  // HomeAllToolsPage(this.option) : super();

  @override
  void onInitState(CommonPageBloc pageBloc) {
    pageBloc.pageHttpGet(
        path:
            //path:"http://172.18.130.29:8080/wtsapi/wts/portal/v1/app/allTools",
            RevisionMainApi.homeAllToolsUrl,
        widgetBuilderByData: (dynamic data) {
          List list = data as List;
          List<ToolsCategoryBean> categoryList;
          if (listNotEmpty(list)) {
            categoryList =
                list.map((i) => ToolsCategoryBean.fromJson(i)).toList();
          } else {
            categoryList = new List();
          }
          if(categoryList.length == 0) {
            return LogicPage.emptyPage(emptyParam: SaleConfig.emptyPageParam);
          }
          return HomeAllToolsWidget(pageBloc, categoryList);
        });
  }
}

class _CategoryBloc extends EventBloc<int> {
  @override
  int get initialState => 0;
}

class HomeAllToolsWidget extends StatefulWidget {
  final CommonPageBloc pageBloc;
  final List<ToolsCategoryBean> dataList; //返回数据
  HomeAllToolsWidget(this.pageBloc, this.dataList, {Key key}) : super(key: key);
  _HomeAllToolsWidgetState createState() => _HomeAllToolsWidgetState();
}

class _HomeAllToolsWidgetState extends State<HomeAllToolsWidget> {
  _CategoryBloc bloc;
  List<ToolsCategoryBean> categoryList;
  double topHeight = 10;
  @override
  void initState() {
    super.initState();
    bloc = _CategoryBloc();
    categoryList = widget.dataList;
    widget.pageBloc.eventBus
        .on<SelectToolCategoryResultEvent>()
        .listen((SelectToolCategoryResultEvent event) {
      bloc?.dispatch(event.index);
    });
  }

  @override
  Widget build(BuildContext context) {
    return getBodyWidget(context); //Scaffold(body: getBodyWidget(context));
  }

  void itemOnTap(ToolsItem item) {
    RouteUtil.goPage(item.appRouteUrl);
     Map<String,dynamic> map = Map<String,dynamic>();
     map["name"] = item.title;
     UserTrack.track("click_main_all_tools_item", map);
  }

  Widget getBodyWidget(BuildContext context) {
    return BlocBuilder(
      bloc: bloc,
      builder: (BuildContext context, int selectedIndex) {
        return Container(
          color: Color(0xffF5F5F5),
          width: 375,
          child: listNotEmpty(categoryList)
              ? Column(
                  children: <Widget>[
                    ToolsCategoryBarWidget(widget.dataList, selectedIndex,
                        widget.pageBloc.eventBus),
                    Expanded(
                      child: IndexedStack(
                        index: selectedIndex,
                        children: categoryList?.map((ToolsCategoryBean bean){
                          return _contentWidget(bean.modules);
                        })?.toList(),
                      ),
                    )
                  ],
                )
              : SizedBox.shrink(),
        );
      },
    );
  }

   //工具内容列表
  Widget _contentWidget(List<ModulesListBean> list) {
    return Stack(
      children: <Widget>[
        NotificationListener(
          onNotification: (ScrollNotification notification) {
            double pix = notification.metrics.pixels;
            if (pix < 0) {
              setState(() {
                topHeight = (-pix) + 10;
              });
            }
            return true;
          },
          child: ListView.builder(
            //业务线工具
            padding: EdgeInsets.only(top: 0),
            scrollDirection: Axis.vertical,
            itemCount: listNotEmpty(list) ? list.length : 0,
            itemBuilder: (BuildContext context, int index) {
              ModulesListBean moduleBean = list[index];
              Widget widget;
              if (index == 0) {
                //第一条元素带有蓝色顶部
                widget = Stack(
                  children: <Widget>[
                    Container(
                      height: 44,
                      decoration: BoxDecoration(
                        color: Color(0xff3274F9),
                        border: Border(
                            top: BorderSide(
                                width: 0.0, color: Color(0xff3274F9))),
                      ),
                    ),
                    Container(
                        margin: EdgeInsets.only(top: 10),
                        child: _toolsCardWidget(moduleBean))
                  ],
                );
              } else {
                widget = _toolsCardWidget(moduleBean);
              }
              return Padding(
                  padding: EdgeInsets.only(bottom: 10), child: widget);
            },
          ),
        ),
        Positioned(
          top: 0,
          left: 0,
          right: 0,
          child: Container(
            height: topHeight,
            decoration: BoxDecoration(
              color: Color(0xff3274F9),
              border:
                  Border(top: BorderSide(width: 0.0, color: Color(0xff3274F9))),
            ),
          ),
        )
      ],
    );
  }

  //工具卡片
  Widget _toolsCardWidget(ModulesListBean moduleBean) {
    return ToolsModuleWidget(
      headChild: ToolsCommonTitleWidget(moduleBean.moduleName),
      backgroundColor: Colors.transparent,
      data: moduleBean.tools,
      borderRadius: 10,
      itemOnTap: (ToolsItem item) {
        itemOnTap(item);
      },
    );
  }
}
